//
//  HPtabBarItem.swift
//  MyWeibo
//
//  Created by apple on 16/4/19.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit

class HPtabBarItem: UITabBarItem {

    // 重写badageValue的set方法, 外界修改数值,就给badge 设置背景
    override var badgeValue: String? {
        
        didSet {
            // 根据键值获取value
//            let target = self.valueForKey("_target") as! UITabBarController
            
            let target = self.valueForKey("_target") as! HPMainViewController
            // 遍历 KVC 赋值
            for subView in target.tabBar.subviews {
                if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for v in subView.subviews {
                        if v.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            for badgeView in v.subviews {
                                if badgeView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    badgeView.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
